<?php
/**
 * Created by PhpStorm.
 * User: YWh
 * Date: 2018/10/28
 * Time: 17:01
 */

namespace App\Http\Controllers\User;

use App\Models\User\LiveScoreWater;
use App\Models\User\LiveUser;
use App\Models\User\LiveUserCard;
use App\Models\User\Vip;
use function Couchbase\defaultDecoder;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Str;

class UserController extends Controller
{
    public function VipCardList ()
    {
        $data = Vip::getVipCardList();
        return self::response($data,200);
    }

    public function create(Request $request)
    {
        $user_id    = (int)$request->input('user_id');
        $id         = (int)$request->input('id');
        $num        = (int)$request->input('num');
        if (empty($user_id)) {
            return self::response(null,-1,'用户id不能为空');
        }
        if (empty($id)) {
            return self::response(null,-2,'充值ID不能为空');
        }
        if (empty($num)) {
            return self::response(null,-3,'请输入要生成的卡密数量');
        }
        if (!is_int($num)) {
            return self::response(null,-4,'卡密数量格式不正确');
        }
        $vip = Vip::getAppointVipById($id);
        if (empty($vip)) {
            return self::response(null,-5,'充值ID不存在');
        }
        $user = LiveUser::getUserByUserId($user_id);
        if (empty($user)) {
            return self::response(null,-6,'用户不存在');
        }
        if ($user->is_agent == 0) {
            return self::response(null,-7,'您还不是代理哦~');
        }
        $total_score = $vip->need_score*$num;
        if ($total_score > $user->user_score) {
            return self::response(null,-8,'您的积分不足哦~');
        }
        $flag1 = LiveUser::modifyScore($user_id,$total_score,2);
        if (!$flag1) {
            return self::response(null,-9,'您的积分不足哦~');
        }
        $data = [];
        $time = time();
        for ($i = 0; $i < $num; $i++) {
            $data[$i]['user_id']        = $user_id;
            $data[$i]['code']           = (string) Str::uuid();
            $data[$i]['vip_time']       = $vip->vip_time;
            $data[$i]['created_time']   = $time;
        }
        $flag2 = LiveUserCard::createCard($data);
        if (!$flag2) {
            return self::response(null,-10,'系统繁忙~');
        }
        LiveScoreWater::createWater($user_id,$total_score,1);
        return self::response(null,200);
    }

    public function cardList(Request $request)
    {
        $user_id    = (int)$request->input('user_id');
        $type       = (int)$request->input('type');
        $page       = (int)$request->input('page',1);
        if (empty($user_id)) {
            return self::response(null,-1,'用户不存在哦~');
        }
        if (empty($type)) {
            return self::response(null,-2,'卡密类型不能为空哦~');
        }
        $data = LiveUserCard::getCardListByUserId($user_id,$type);
        return self::response($data,200);
    }

    public function activateList(Request $request)
    {
        $user_id    = (int)$request->input('user_id');
        if (empty($user_id)) {
            return self::response(null,-1,'用户不存在哦~');
        }
        $data = LiveUserCard::getUseCardByUserId($user_id);
        return self::response($data,200);
    }

    public function transferScore(Request $request)
    {
        $user_id    = (int)$request->input('user_id');
        $to_user_id = (int)$request->input('to_user_id');
        $score      = (int)$request->input('score');
        if (empty($user_id)) {
            return self::response(null,-1,'用户不存在哦~');
        }
        if (empty($to_user_id)) {
            return self::response(null,-2,'下级用户不存在哦~');
        }
        if (empty($score)) {
            return self::response(null,-3,'请输入积分数量哦~');
        }
        if ($user_id === $to_user_id) {
            return self::response(null,-4,'您不能自己给自己转积分哦~');
        }
        $user = LiveUser::getUserByUserId($user_id);
        if (empty($user)) {
            return self::response(null,-5,'用户不存在哦~');
        }
        if ($user->is_agent == 0) {
            return self::response(null,-6,'您还不是代理哦~');
        }
        $to_user = LiveUser::getUserByUserId($to_user_id);
        if (empty($to_user)) {
            return self::response(null,-7,'您的下级用户不存在哦~');
        }
        if ($to_user->invite_user_id !== $user_id) {
            return self::response(null,-8,'他还不是您的下级用户哦~');
        }
        if ($to_user->is_agent == 0) {
            return self::response(null,-9,'您的下级用户还不是代理哦~');
        }
        if ($score > $user->user_score) {
            return self::response(null,-10,'您的积分不够哦~');
        }
        $flag1 = LiveUser::modifyScore($user_id,$score,2);
        if (!$flag1) {
            return self::response(null,-11,'您的积分不够哦~');
        }
        $flag2 = LiveUser::modifyScore($to_user_id,$score,1);
        if (!$flag2) {
            return self::response(null,-12,'系统繁忙，请稍后再试哦~');
        }
        LiveScoreWater::createWater($user_id,$score,2);
        LiveScoreWater::createWater($to_user_id,$score,3);
        return self::response(null,200);
    }

    public function openAgent(Request $request)
    {
        $user_id    = (int)$request->input('user_id');
        $to_user_id = (int)$request->input('to_user_id');
        $code       = trim($request->input('code',''));
        $phone      = trim($request->input('phone',''));
        if (empty($user_id)) {
            return self::response(null,-1,'用户不存在哦~');
        }
        if (empty($to_user_id)) {
            return self::response(null,-2,'下级用户不存在哦~');
        }
        if (empty($code)) {
            return self::response(null,-3,'请输入邀请码哦~');
        }
        if (mb_strlen($code,'utf-8') < 4) {
            return  self::response(null,-4,'邀请码不能少于4位哦');
        }
        if (mb_strlen($code,'utf-8') > 20) {
            return  self::response(null,-5,'邀请码不能大于20位哦');
        }
        $flag1 = LiveUser::getUserIDByInviteCode($code);
        if ($flag1) {
            return self::response(null,-6,'邀请码已被使用了哦~');
        }
        if (empty($phone)) {
            return self::response(null,-7,'请输入联系方式哦~');
        }
        if (mb_strlen($phone,'utf-8') > 50) {
            return self::response(null,-8,'联系方式位数不能超过50位哦~');
        }
        $user = LiveUser::getUserByUserId($user_id);
        if (empty($user)) {
            return self::response(null,-9,'用户不存在哦~');
        }
        if ($user->is_agent == 0) {
            return self::response(null,-10,'您还不是代理哦~');
        }
        $to_user = LiveUser::getUserByUserId($to_user_id);
        if (empty($to_user)) {
            return self::response(null,-11,'您的下级用户不存在哦~');
        }
        if ($to_user->invite_user_id !== $user_id) {
            return self::response(null,-12,'他还不是您的下级用户哦~');
        }
        if ($to_user->is_agent == 1) {
            return self::response(null,-13,'您的下级用户已经是代理了哦~');
        }
        $flag2 = LiveUser::openAgent($to_user_id,$phone,$code);
        if (!$flag2) {
            return self::response(null,-14,'系统繁忙，请稍后再试哦~');
        }
        return self::response(null,200);
    }

    public function agentInfo(Request $request)
    {
        $user_id    = (int)$request->input('user_id');
        if (empty($user_id)) {
            return self::response(null,-1,'用户不存在哦~');
        }
        $user = LiveUser::getUserByUserId($user_id);
        if (empty($user)) {
            return self::response(null,-2,'访问用户不存在哦~');
        }
        if ($user->is_agent == 0) {
            return self::response(null,-3,'您还不是代理哦~');
        }
        unset($user->user_password);
        unset($user->user_token);
        $user->agent_count = LiveUser::countUserAgent($user_id);
        return self::response($user,200);
    }
}